package com.tianqu.tms.entity;

import java.io.File;

import com.tianqu.tms.core.base.TmsEntity;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.util.CopyFile;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.ImageUtils;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.TmsProxyPicDao;
import com.tianqu.tms.dto.TmsProxyPicDto;

/**
 * 待办车主资料照片entity
 * 
 * @author Administrator
 * 
 */
public class TmsProxyPic extends TmsEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2606071817028254053L;

	private String serial;//

	private String ownersGroupPhoto;// 车主合影
	private String materialImages1;// 资料照片1
	private String materialImages2;// 资料照片2
	private String materialImages3;// 资料照片3
	private String materialImages4;// 资料照片4
	private String materialImages5;// 资料照片5
	private String insertTime;// 受理时间
	private int insertUserId;//
	private String updateTime;//
	private int updateUserId;//

	public static TmsProxyPic getInstance() {
		TmsProxyPic proxy = new TmsProxyPic();
		proxy.setExisting(false);
		return proxy;
	}
	
	/**
	 * 根据车辆ID查询图片信息
	 * @throws TmsDaoException 
	 */
	public TmsProxyPic findPicInfoBySerial(String serial) throws TmsDaoException
	{
		TmsProxyPicDao mDao = (TmsProxyPicDao) TmsDao.getInstance(TmsDao.TMS_PROXY_PIC);
		
		
		TmsProxyPicDto dto = mDao.findPicInfoBySerial(serial);
		
		TmsProxyPic entity = new TmsProxyPic();
		
		entity.setSerial(serial);
		entity.setOwnersGroupPhoto(dto.getOwnersGroupPhoto());
		entity.setMaterialImages1(dto.getMaterialImages1());
		entity.setMaterialImages2(dto.getMaterialImages2());
		entity.setMaterialImages3(dto.getMaterialImages3());
		entity.setMaterialImages4(dto.getMaterialImages4());
		entity.setMaterialImages5(dto.getMaterialImages5());
		
		return entity;
		
	}
	
	/**
	 * 插入待办资料图片信息
	 * @throws TmsDaoException 
	 * 
	 */
	public void insertProxyPicInfo(TmsProxyPic entity,String sysPath) throws TmsDaoException
	{
		TmsProxyPicDao mDao = (TmsProxyPicDao) TmsDao.getInstance(TmsDao.TMS_PROXY_PIC);
		
		//开始更新
		//将image图片信息放到服务器上
		//对每个path进行处理   。。。【photo就是imgpath
		File outFile = null;
		File inFile = null;
		
		sysPath = sysPath.replace("/tmsweb", TmsConstants.BLANK);
		
		TmsProxyPicDto dto = new TmsProxyPicDto();

		if(entity.getMaterialImages1()!=null && !"".equals(entity.getMaterialImages1()))
		{
			inFile = new File(sysPath+entity.getMaterialImages1());
			outFile = new File(ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_ZL_IMAGE_KEY)+ entity.getSerial()+"/")+ entity.getSerial()+"_1.jpg");
			CopyFile.copy(inFile, outFile);	
			
			//
			dto.setMaterialImages1(imgPath(entity.getMaterialImages1(), entity.getSerial(), "1"));
		}
		if(entity.getMaterialImages2()!=null && !"".equals(entity.getMaterialImages2()))
		{
			inFile = new File(sysPath+entity.getMaterialImages2());
			outFile = new File(ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_ZL_IMAGE_KEY)+"/"+entity.getSerial()+"/")+entity.getSerial()+"_2.jpg");
			CopyFile.copy(inFile, outFile);	
			
			//
			dto.setMaterialImages2(imgPath(entity.getMaterialImages2(),entity.getSerial(), "2"));
		}
		if(entity.getMaterialImages3()!=null && !"".equals(entity.getMaterialImages3()))
		{
			inFile = new File(sysPath+entity.getMaterialImages3());
			outFile = new File(ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_ZL_IMAGE_KEY)+"/"+entity.getSerial()+"/")+entity.getSerial()+"_3.jpg");
			CopyFile.copy(inFile, outFile);	
			
			//
			dto.setMaterialImages3(imgPath(entity.getMaterialImages3(), entity.getSerial(), "3"));
		}
		if(entity.getMaterialImages4()!=null && !"".equals(entity.getMaterialImages4()))
		{
			inFile = new File(sysPath+entity.getMaterialImages4());
			outFile = new File(ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_ZL_IMAGE_KEY)+"/"+entity.getSerial()+"/")+entity.getSerial()+"_4.jpg");
			CopyFile.copy(inFile, outFile);	
			
			//
			dto.setMaterialImages4(imgPath(entity.getMaterialImages4(), entity.getSerial(), "4"));
		}
		if(entity.getMaterialImages5()!=null && !"".equals(entity.getMaterialImages5()))
		{
			inFile = new File(sysPath+entity.getMaterialImages5());
			outFile = new File(ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_ZL_IMAGE_KEY)+"/"+entity.getSerial()+"/")+entity.getSerial()+"_5.jpg");
			CopyFile.copy(inFile, outFile);	
			
			//
			dto.setMaterialImages5(imgPath(entity.getMaterialImages5(), entity.getSerial(), "5"));
		}
		if(entity.getOwnersGroupPhoto()!=null && !"".equals(entity.getOwnersGroupPhoto()))
		{
			inFile = new File(sysPath+entity.getOwnersGroupPhoto());
			outFile = new File(ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_ZL_IMAGE_KEY)+"/"+entity.getSerial()+"/")+entity.getSerial()+"_5.jpg");
			CopyFile.copy(inFile, outFile);	
			//
			dto.setOwnersGroupPhoto(imgPath(entity.getOwnersGroupPhoto(), entity.getSerial(), "6"));
		}
		dto.setSerial(entity.getSerial());
		dto.setInsertUserId(entity.getInsertUserId());
		//这个地方需要对图片路径进行转换	
		//开始进行存储
		mDao.insertProxyPicInfo(dto);
		
	}
	
	/**
	 * 更新图片表
	 */
	public void updateProxyInfo(TmsProxyPic entity,String sysPath) throws TmsDaoException
	{
		TmsProxyPicDao mDao = (TmsProxyPicDao) TmsDao.getInstance(TmsDao.TMS_PROXY_PIC);
		
		//开始更新
		//将image图片信息放到服务器上
		//对每个path进行处理   。。。【photo就是imgpath
		File outFile = null;
		File inFile = null;
		
		sysPath = sysPath.replace("/tmsweb", TmsConstants.BLANK);
		
		TmsProxyPicDto dto = new TmsProxyPicDto();

		if(entity.getMaterialImages1()!=null && !"".equals(entity.getMaterialImages1()))
		{
			inFile = new File(sysPath+entity.getMaterialImages1());
			if(entity.getMaterialImages1().indexOf(entity.getSerial()) == -1)
			{
				outFile = new File(ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_ZL_IMAGE_KEY)+entity.getSerial()+"/")+entity.getSerial()+"_1.jpg");
				CopyFile.copy(inFile, outFile);	
			}
			//
			dto.setMaterialImages1(imgPath(entity.getMaterialImages1(), entity.getSerial(), "1"));
		}
		if(entity.getMaterialImages2()!=null && !"".equals(entity.getMaterialImages2()))
		{
			inFile = new File(sysPath+entity.getMaterialImages2());
			if(entity.getMaterialImages2().indexOf(entity.getSerial()) == -1)
			{			
				outFile = new File(ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_ZL_IMAGE_KEY)+"/"+entity.getSerial()+"/")+entity.getSerial()+"_2.jpg");
				CopyFile.copy(inFile, outFile);	
			}
			//
			dto.setMaterialImages2(imgPath(entity.getMaterialImages2(),entity.getSerial(), "2"));
		}
		if(entity.getMaterialImages3()!=null && !"".equals(entity.getMaterialImages3()))
		{
			inFile = new File(sysPath+entity.getMaterialImages3());
			if(entity.getMaterialImages3().indexOf(entity.getSerial()) == -1)
			{				
				 
				outFile = new File(ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_ZL_IMAGE_KEY)+"/"+entity.getSerial()+"/")+entity.getSerial()+"_3.jpg");
				
				CopyFile.copy(inFile, outFile);	
			}
			//
			dto.setMaterialImages3(imgPath(entity.getMaterialImages3(), entity.getSerial(), "3"));
		}
		if(entity.getMaterialImages4()!=null && !"".equals(entity.getMaterialImages4()))
		{
			inFile = new File(sysPath+entity.getMaterialImages4());
			if(entity.getMaterialImages4().indexOf(entity.getSerial()) == -1)
			{				
				 
				outFile = new File(ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_ZL_IMAGE_KEY)+"/"+entity.getSerial()+"/")+entity.getSerial()+"_4.jpg");
				
				CopyFile.copy(inFile, outFile);	
			}
			//
			dto.setMaterialImages4(imgPath(entity.getMaterialImages4(), entity.getSerial(), "4"));
		}
		if(entity.getMaterialImages5()!=null && !"".equals(entity.getMaterialImages5()))
		{
			inFile = new File(sysPath+entity.getMaterialImages5());
			if(entity.getMaterialImages5().indexOf(entity.getSerial()) == -1)
			{			
				 
				outFile = new File(ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_ZL_IMAGE_KEY)+"/"+entity.getSerial()+"/")+entity.getSerial()+"_5.jpg");
				
				CopyFile.copy(inFile, outFile);	
			}
			//
			dto.setMaterialImages5(imgPath(entity.getMaterialImages5(), entity.getSerial(), "5"));
		}
		if(entity.getOwnersGroupPhoto()!=null && !"".equals(entity.getOwnersGroupPhoto()))
		{
			inFile = new File(sysPath+entity.getOwnersGroupPhoto());
			if(entity.getOwnersGroupPhoto().indexOf(entity.getSerial()) == -1)
			{				
				 
				outFile = new File(ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_ZL_IMAGE_KEY)+"/"+entity.getSerial()+"/")+entity.getSerial()+"_5.jpg");
				
				CopyFile.copy(inFile, outFile);	
			}
			//
			dto.setOwnersGroupPhoto(imgPath(entity.getOwnersGroupPhoto(), entity.getSerial(), "6"));
		}
		dto.setSerial(entity.getSerial());
		dto.setUpdateUserId(entity.getUpdateUserId());
		//这个地方需要对图片路径进行转换	
		
		mDao.updateProxyInfo(dto);
		
	}
	
	
	/**
	 * 处理存入数据库中的图片路径
	 */
	private String imgPath(String path,String serial,String imgKey){
		return  ImageUtils.getImageUrl(Registry.getConfig(TmsConstants.UPLOAD_ZL_IMAGE_KEY) +serial+"/"+serial+"_"+imgKey+".jpg");	
	}
	
	

	@Override
	public void save() throws TmsDaoException, TmsException {
		// TODO Auto-generated method stub

	}

	public String getSerial() {
		return serial;
	}

	public void setSerial(String serial) {
		this.serial = serial;
	}

	public String getOwnersGroupPhoto() {
		return ownersGroupPhoto;
	}

	public void setOwnersGroupPhoto(String ownersGroupPhoto) {
		this.ownersGroupPhoto = ownersGroupPhoto;
	}

	public String getMaterialImages1() {
		return materialImages1;
	}

	public void setMaterialImages1(String materialImages1) {
		this.materialImages1 = materialImages1;
	}

	public String getMaterialImages2() {
		return materialImages2;
	}

	public void setMaterialImages2(String materialImages2) {
		this.materialImages2 = materialImages2;
	}

	public String getMaterialImages3() {
		return materialImages3;
	}

	public void setMaterialImages3(String materialImages3) {
		this.materialImages3 = materialImages3;
	}

	public String getMaterialImages4() {
		return materialImages4;
	}

	public void setMaterialImages4(String materialImages4) {
		this.materialImages4 = materialImages4;
	}

	public String getMaterialImages5() {
		return materialImages5;
	}

	public void setMaterialImages5(String materialImages5) {
		this.materialImages5 = materialImages5;
	}

	public String getInsertTime() {
		return insertTime;
	}

	public void setInsertTime(String insertTime) {
		this.insertTime = insertTime;
	}

	public String getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(String updateTime) {
		this.updateTime = updateTime;
	}

	public int getInsertUserId() {
		return insertUserId;
	}

	public void setInsertUserId(int insertUserId) {
		this.insertUserId = insertUserId;
	}

	public int getUpdateUserId() {
		return updateUserId;
	}

	public void setUpdateUserId(int updateUserId) {
		this.updateUserId = updateUserId;
	}

}
